-- This file is hereby placed in the Public Domain
local addonName, ns = ...
local addon = ns[addonName]
local oUF = ns.oUF

local tags = {}
addon.tags = tags
local utils = addon.utils
local s = addon.cfg
local colors = addon.colors

tags.name = '[difficulty][smartlevel]|r [raidcolor][name4]|r'
tags.majorname = '[raidcolor][name]|r'
tags.hp = '[colorhealth][hp]|r'
tags.mp = '[colorpower][mp]|r'
tags.lvl = '[difficulty][level]|r'
tags.curhp = '[colorhealth][curhp]|r'
tags.curmp = '[colorpower][curpp]|r'
tags.smartHP = '[smartHP]'
tags.smartMP = '[smartMP]'

for i = 1, 10 do
    local token = 'name' .. i
    tags[token] = '[raidcolor][' .. token .. ']|r'
    oUF.Tags.Methods[token] = function(u)
        local n = UnitName(u)
        if(not n) then return end

        if(strbyte(n,1) > 224) then
            return utils.utf8sub(n, i)
        else
            return utils.utf8sub(n, i*2)
        end
    end
end

oUF.Tags.difficulty = [=[function(u)
    local l = UnitLevel(u)
    return Hex(GetQuestDifficultyColor((l>0) and l or 99))
end]=]

oUF.Tags.colorhealth = [=[function(u)
    return Hex(_COLORS.health)
end]=]

oUF.Tags.colorpower = [=[function(u)
    local _, t = UnitPowerType(u)
    local c = t and _COLORS.power[t]
    if(c) then
        return Hex(c)
    end
end]=]

oUF.Tags.hp = function(u)
    local hp = UnitHealth(u)
    return hp and utils.truncate(hp)
end

oUF.Tags.mp = function(u)
    local mp = UnitPower(u)
    return mp and utils.truncate(mp)
end

--oUF.TagEvents["smartHP"] = 'UNIT_HEALTH UNIT_MAXHEALTH'
oUF.Tags.Events["smartHP"] = 'UNIT_HEALTH UNIT_MAXHEALTH'
--oUF.Tags["smartHP"] = function(unit)
oUF.Tags.Methods["smartHP"] = function(unit)
	local min, max = UnitHealth(unit), UnitHealthMax(unit)
	if not max then return end
	if max == 0 then max = 1 end
	local r, g, b = oUF.ColorGradient(min/max, 1, 245/255, 68/255, 68/255, 245/255, 186/255, 69/255, 105/255, 201/255, 105/255)
	local status = not UnitIsConnected(unit) and 'Offline' or UnitIsGhost(unit) and 'Ghost' or UnitIsDead(unit) and 'Dead' or UnitIsFeignDeath(unit) and '|cffFF3333FD|r'
	return status and status or 
		((unit == 'target' or unit == 'player') and min~=max) and format('|cffFD5C69%d|r |cffDBF6DB-|r |cff%02x%02x%02x%d%%|r', min, r * 255, g * 255, b * 255, floor(min / max * 100)) or
		(unit == 'pet' and min~=max) and format('|cffFD5C69%s|r', utils.ShortValue(min)) or
		(min~=max) and format('|cffFD5C69%d|r|cff98C290%%|r', floor(min / max * 100)) or ('|cff98C290'..max)
end

--oUF.TagEvents["smartMP"] = oUF.TagEvents["curpp"]
--oUF.Tags["smartMP"] = function(unit)
oUF.Tags.Events["smartMP"] = oUF.Tags.Events["curpp"]
oUF.Tags.Methods["smartMP"] = function(unit)
	local _, str = UnitPowerType(unit)
	local min = UnitMana(unit)
	return (not UnitIsPlayer(unit) or not UnitIsConnected(unit) or UnitIsDeadOrGhost(unit) or min == 0) and ""
	or ('%s%d|r'):format(Hex(colors.power[str] or {1, 1, 1}), oUF.Tags.Methods["curpp"](unit) )
     end

oUF.Tags.Events["diffcolor"] = 'UNIT_LEVEL'
oUF.Tags.Methods["diffcolor"]  = function(unit)
	local r, g, b
	local level = UnitLevel(unit)
	if (level < 1) then
		r, g, b = 245/255, 68/255, 68/255, 245/255
	else
		local diffcolor = UnitLevel('target') - UnitLevel('player')
		if (diffcolor >= 5) then
			r, g, b = 245/255, 68/255, 68/255, 245/255
		elseif (diffcolor >= 3) then
			r, g, b = 245/255, 88/255, 24/255
		elseif (diffcolor >= -2) then
			r, g, b = 245/255, 186/255, 69/255
		elseif (-diffcolor <= GetQuestGreenRange()) then
			r, g, b = 105/255, 201/255, 105/255
		else
			r, g, b = 0.55, 0.57, 0.61
		end
	end
	return string.format('|cff%02x%02x%02x', r * 255, g * 255, b * 255)
     end

oUF.Tags.Events["smartINFO"] = 'UNIT_NAME_UPDATE UNIT_REACTION UNIT_FACTION'
oUF.Tags.Methods["smartINFO"] = function(unit)
	if (UnitIsTapped(unit) and not UnitIsTappedByPlayer(unit)) then
		return Hex(colors.tapped)
	elseif (not UnitIsConnected(unit)) then
		return Hex(colors.disconnected)
	     elseif (not UnitIsPlayer(unit)) and colors.reaction[UnitReaction(unit, 'player')] then
	   return Hex(colors.reaction[UnitReaction(unit, 'player')])
	elseif UnitIsPlayer(unit) then
	   return Hex(RAID_CLASS_COLORS[select(2, UnitClass(unit))])
	elseif (UnitFactionGroup(unit) and UnitIsEnemy(unit, 'player') and UnitIsPVP(unit)) then
	   return Hex(colors.red)
	else
	   return Hex(UnitSelectionColor(unit))
	end
     end

oUF.Tags.Events["shortlarge"]   = 'UNIT_NAME_UPDATE'
oUF.Tags.Methods["shortlarge"]  = function(unit)
	local n = UnitName(unit)
	return utils.utf8sub(n, 12, true)
     end

oUF.Tags.Events["customthreat"] = 'UNIT_THREAT_LIST_UPDATE'
oUF.Tags.Methods["customthreat"] = function()
	local _, _, perc = UnitDetailedThreatSituation('player', 'target')
	return perc and ('%s%d%%|r'):format(Hex(GetThreatStatusColor(UnitThreatSituation('player', 'target'))), perc)
     end

oUF.Tags.Events["druidpower"] = 'UNIT_MAXPOWER UNIT_POWER UPDATE_SHAPESHIFT_FORM'--'UNIT_MANA UPDATE_SHAPESHIFT_FORM'
oUF.Tags.Methods["druidpower"] = function(unit)
	local min, max = UnitPower(unit, 0), UnitPowerMax(unit, 0)
	return UnitPowerType(unit) ~= 0 and format('|cff0090ff%d - %d%%|r', min, math.floor(min / max * 100))
end